package neo.app.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import neo.core.common.PagingList;
import neo.core.util.MD5;
import neo.core.util.MapUtil;

import org.springframework.dao.EmptyResultDataAccessException;

public class UserService extends BaseService {

	//通过用户名获取用户信息
	private static final String SQL_GET_USER_BY_USER_NAME = "select * from ken_user join ken_employee on user_employee_id=employee_id where employee_no = ?";

	/**
	 * 通过用户名获取用户信息
	 */
	public Map getUserByUserName(String userName) {
		try {
			return jt.queryForMap(SQL_GET_USER_BY_USER_NAME, new Object[] { userName });
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	//获取全部用户信息
	private static final String SQL_GET_ALL_USERS = "select * from ken_user,ken_employee where user_employee_id=employee_id";

	/**
	 * 获取全部用户信息
	 */
	public PagingList getAllUsers() {
		return getPagingList(SQL_GET_ALL_USERS);
	}

	//获取某角色指定权限的SQL
	private static final String SQL_GET_USER_AUTH = "SELECT `acl_key`, IF(`racl_type` != '1', 'FALSE', IFNULL(`racl_value`, `acl_default`)) AS `acl_value` FROM `ken_user`, `ken_urole`, `ken_role`, `ken_racl`, `ken_acl` WHERE `racl_role_id` = `role_id` AND `role_id` = `urole_role_id` AND `user_id` = `urole_user_id` AND `racl_acl_id` = `acl_id` AND `user_id` = ?";

	/**
	 * 根据用户ID和权限标识获取权限设定值
	 * @param uID
	 * @param authKey
	 * @return
	 */
	public Map getAuth(int uID) {
		try {
			List temp = jt.queryForList(SQL_GET_USER_AUTH, new Object[] { uID });
//log.debug(SQL_GET_USER_AUTH + uID);			
//log.debug(temp.get(0) +"temp");			
			Map resultMap = new HashMap();
			for (int i = 0; i < temp.size(); i++) {
				Map map = (Map)temp.get(i);
				resultMap.put((String) map.get("acl_key"), (String) map.get("acl_value"));
			}
			return resultMap;
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	//添加用户
	private static final String SQL_INSERT_INTO_USER = "insert into ken_user values(user_id,?,?)";
	//根据员工编号查找 员工id
	private static final String SQL_GET_ID_BY_EMPLOYEE_NO = "select employee_id from ken_employee where employee_no=?";
	//根据员工id查询user
	private static final String SQL_GET_USER_BY_EMPLOYEE_ID = "select * from ken_user where user_employee_id=?";
	/**
	 * 添加用户
	 * @param parameters
	 * @return
	 */
	public boolean addUser(Map map) {
		//1、获取值
		String employee_no = MapUtil.getStringFromMap(map, "employee_no");
		String user_password = MapUtil.getStringFromMap(map, "user_password");
		//2、校验有效性 (没做)
		user_password = MD5.getMD5ofStr(user_password);//密码加密
		int employee_id;
		//根据员工编号，查找emloyee_id
		List list = new ArrayList();
		list = jt.queryForList(SQL_GET_ID_BY_EMPLOYEE_NO, new Object[]{employee_no });
		if(list.size() == 0){
			return false;
		}
		else{
			employee_id = Integer.parseInt(MapUtil.getStringFromMap((Map)list.get(0), "employee_id"));
			//检查 此员工是否已经在 用户表中存在
			if(jt.queryForList(SQL_GET_USER_BY_EMPLOYEE_ID, new Object[]{employee_id }).size() > 0){
				return false;//已存在
			}
		}
		
		if(jt.update(SQL_INSERT_INTO_USER, new Object[]{employee_id, user_password })>0){
			return true;
		}
		return false;
	}
}
